import { defineStore } from 'pinia'
import { ref } from 'vue'
import { request } from '@/utils/request'

export interface RentalOrder {
  id: number
  orderNo: string
  userId: number
  equipmentId: number
  rentalStartDate: string
  rentalEndDate: string
  plannedReturnDate: string
  actualReturnDate?: string
  dailyRentPrice: number
  totalAmount: number
  depositAmount: number
  status: 'PENDING' | 'ACTIVE' | 'COMPLETED' | 'CANCELLED' | 'OVERDUE'
  notes: string
  createdAt: string
  updatedAt: string
}

export interface RentalStatistics {
  pending: number
  active: number
  completed: number
  cancelled: number
  overdue: number
}

export const useRentalStore = defineStore('rental', () => {
  const rentalList = ref<RentalOrder[]>([])
  const statistics = ref<RentalStatistics>({
    pending: 0,
    active: 0,
    completed: 0,
    cancelled: 0,
    overdue: 0
  })
  const loading = ref(false)

  const fetchRentalList = async () => {
    loading.value = true
    try {
      const data = await request.get('/orders')
      rentalList.value = data
    } catch (error) {
      console.error('获取租赁列表失败:', error)
      throw error
    } finally {
      loading.value = false
    }
  }

  const fetchStatistics = async () => {
    try {
      const data = await request.get('/orders/statistics')
      statistics.value = data
    } catch (error) {
      console.error('获取租赁统计失败:', error)
      throw error
    }
  }

  const getUserRentals = async (userId: number) => {
    loading.value = true
    try {
      const data = await request.get(`/orders/user/${userId}`)
      return data
    } catch (error) {
      console.error('获取用户租赁记录失败:', error)
      throw error
    } finally {
      loading.value = false
    }
  }

  const getCurrentUserRentals = async () => {
    loading.value = true
    try {
      const data = await request.get('/orders/my-rentals')
      return data
    } catch (error) {
      console.error('获取当前用户租赁记录失败:', error)
      throw error
    } finally {
      loading.value = false
    }
  }

  const createRental = async (rental: Partial<RentalOrder>) => {
    loading.value = true
    try {
      const data = await request.post('/orders', rental)
      return data
    } catch (error) {
      console.error('创建租赁订单失败:', error)
      throw error
    } finally {
      loading.value = false
    }
  }

  const approveOrder = async (orderId: number) => {
    loading.value = true
    try {
      const data = await request.put(`/orders/${orderId}/approve`)
      return data
    } catch (error) {
      console.error('审批订单失败:', error)
      throw error
    } finally {
      loading.value = false
    }
  }

  const returnEquipment = async (orderId: number) => {
    loading.value = true
    try {
      const data = await request.put(`/orders/${orderId}/return`)
      return data
    } catch (error) {
      console.error('归还设备失败:', error)
      throw error
    } finally {
      loading.value = false
    }
  }

  return {
    rentalList,
    statistics,
    loading,
    fetchRentalList,
    fetchStatistics,
    getUserRentals,
    getCurrentUserRentals,
    createRental,
    approveOrder,
    returnEquipment
  }
})